package com.ass.wechat.web;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ass.base.web.BaseController;
import com.ass.wechat.service.CoreService;
import com.ass.wechat.utils.SignUtil;



@Controller
@RequestMapping("/core")
public class CoreController extends BaseController{

	@Resource
	private CoreService coreService;
	
	//接收post请求
	@RequestMapping(value="/core", method=RequestMethod.POST)
	public void applyPost(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("UTF-8");  
		String respMessage = coreService.processRequest(request);
		this.printStr(response, respMessage);
		
	}
	
	
	//接收get请求
	@RequestMapping(value="/core", method=RequestMethod.GET)
	public void applyGet(HttpServletRequest request, HttpServletResponse response){
		
		// 微信加密签名  
		String signature = request.getParameter("signature");  
		// 时间戳  
		String timestamp = request.getParameter("timestamp");  
		// 随机数  
		String nonce = request.getParameter("nonce");  
		// 随机字符串  
		String echostr = request.getParameter("echostr");  
		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败  
		SignUtil su = new SignUtil();
		if (su.checkSignature(signature, timestamp, nonce)) {
			     this.printStr(response, echostr);
			     return;
		}
			
	}
	
}
